package com.biglead.mini.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.biglead.common.utils.DateUtil;
import com.biglead.mini.mapper.HomeDao;
import com.biglead.mini.mapper.SmsFlashPromotionMapper;
import com.biglead.mini.mapper.SmsFlashPromotionSessionMapper;
import com.biglead.mini.mapper.SmsHomeAdvertiseMapper;
import com.biglead.mini.pojo.*;
import com.biglead.mini.result.HomeContentResult;
import com.biglead.mini.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeDao homeDao;

    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;

    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    @Override
    public List<PmsProduct> hotProductList(Integer pageNum, Integer pageSize) {
        return homeDao.getHotProductList(pageNum, pageSize);
    }

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(getHomeAdvertiseList());
        //获取推荐品牌
        result.setBrandList(homeDao.getRecommendBrandList(0, 6));
        //获取秒杀信息
        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
        result.setNewProductList(homeDao.getNewProductList(0, 4));
        //获取人气推荐
        result.setHotProductList(homeDao.getHotProductList(0, 4));
        //获取推荐专题
        result.setSubjectList(homeDao.getRecommendSubjectList(0, 4));
        return result;
    }

    @Override
    public Integer goodsCount() {
        return homeDao.getGoodsCount();
    }

    private HomeFlashPromotion getHomeFlashPromotion() {
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        //获取当前秒杀活动
        Date now = new Date();
        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
        if (flashPromotion != null) {
            //获取当前秒杀场次
            SmsFlashPromotionSession flashPromotionSession = getFlashPromotionSession(now);
            if (flashPromotionSession != null) {
                homeFlashPromotion.setStartTime(flashPromotionSession.getStartTime());
                homeFlashPromotion.setEndTime(flashPromotionSession.getEndTime());
                //获取下一个秒杀场次
                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(homeFlashPromotion.getStartTime());
                if(nextSession!=null){
                    homeFlashPromotion.setNextStartTime(nextSession.getStartTime());
                    homeFlashPromotion.setNextEndTime(nextSession.getEndTime());
                }
                //获取秒杀商品
                List<FlashPromotionProduct> flashProductList = homeDao.getFlashProductList(flashPromotion.getId(), flashPromotionSession.getId());
                homeFlashPromotion.setProductList(flashProductList);
            }
        }
        return homeFlashPromotion;
    }
    //根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date date) {
        Date currDate = DateUtil.getDate(date);

        List<SmsFlashPromotion> flashPromotionList  =smsFlashPromotionMapper.selectList(new LambdaQueryWrapper<SmsFlashPromotion>()
                .eq(SmsFlashPromotion::getStatus,1)
                .le(SmsFlashPromotion::getStartDate,currDate)
                .ge(SmsFlashPromotion::getEndDate,currDate)
        );
        if (!CollectionUtils.isEmpty(flashPromotionList)) {
            return flashPromotionList.get(0);
        }
        return null;
    }
    //根据时间获取秒杀场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date date) {
        Date currTime = DateUtil.getTime(date);

        List<SmsFlashPromotionSession> promotionSessionList = smsFlashPromotionSessionMapper.selectList(new LambdaQueryWrapper<SmsFlashPromotionSession>()
                .le(SmsFlashPromotionSession::getStartTime,currTime)
                .ge(SmsFlashPromotionSession::getEndTime,currTime)
        );
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }
    //获取下一个场次信息
    private SmsFlashPromotionSession getNextFlashPromotionSession(Date date) {

        List<SmsFlashPromotionSession> promotionSessionList = smsFlashPromotionSessionMapper.selectList(new LambdaQueryWrapper<SmsFlashPromotionSession>()
                .gt(SmsFlashPromotionSession::getStartTime,date)
                .orderByAsc(SmsFlashPromotionSession::getStartTime)

        );
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }

    private List<SmsHomeAdvertise> getHomeAdvertiseList() {

        return smsHomeAdvertiseMapper.selectList(new LambdaQueryWrapper<SmsHomeAdvertise>()
                .eq(SmsHomeAdvertise::getStatus, 1)
                .orderByDesc(SmsHomeAdvertise::getId));
    }

}
